package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduAssignmentQuestionDTO;
import com.zenith.edu.po.vo.EduAssignmentQuestionVO;
import com.zenith.edu.service.EduAssignmentQuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/assignment-question")
@Tag(name = "作业题目管理", description = "作业题目管理接口")
public class EduAssignmentQuestionController {
    
    @Autowired
    private EduAssignmentQuestionService eduAssignmentQuestionService;
    
    @SecurePostMapping(value = "/createAssignmentQuestion",roles = {"ADMIN"},permission = "assignment-question:create")
    @Operation(summary = "创建作业题目")
    public Result<EduAssignmentQuestionVO> createAssignmentQuestion(@RequestBody EduAssignmentQuestionDTO dto) {
        EduAssignmentQuestionVO assignmentQuestionVO = eduAssignmentQuestionService.createAssignmentQuestion(dto);
        return Result.success(assignmentQuestionVO);
    }
    
    @SecurePostMapping(value = "/{id}",roles = "ROLE_ADMIN",permission = "role:read")
    @Operation(summary = "根据ID获取作业题目详情")
    public Result<EduAssignmentQuestionVO> getAssignmentQuestionById(@PathVariable Long id) {
        EduAssignmentQuestionVO assignmentQuestionVO = eduAssignmentQuestionService.getAssignmentQuestionById(id);
        if (assignmentQuestionVO == null) {
            return Result.fail("作业题目不存在");
        }
        return Result.success(assignmentQuestionVO);
    }
    
    @SecurePostMapping(value = "/updateAssignmentQuestion",roles = {"ADMIN"},permission = "assignment-question:update")
    @Operation(summary = "更新作业题目信息")
    public Result<EduAssignmentQuestionVO> updateAssignmentQuestion(@RequestBody EduAssignmentQuestionDTO dto) {
        EduAssignmentQuestionVO assignmentQuestionVO = eduAssignmentQuestionService.updateAssignmentQuestion(dto);
        return Result.success(assignmentQuestionVO);
    }
    
    @SecurePostMapping(value = "/deleteAssignmentQuestion/{id}",roles = {"ADMIN"},permission = "assignment-question:delete")
    @Operation(summary = "删除指定作业题目")
    public Result<Void> deleteAssignmentQuestion(@PathVariable Long id) {
        boolean result = eduAssignmentQuestionService.deleteAssignmentQuestion(id);
        if (!result) {
            return Result.fail("作业题目删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/listAssignmentQuestion",roles = "ROLE_ADMIN",permission = "assignmentQuestion:read")
    @Operation(summary = "获取作业题目列表")
    public Result<List<EduAssignmentQuestionVO>> getAssignmentQuestionList() {
        List<EduAssignmentQuestionVO> assignmentQuestionList = eduAssignmentQuestionService.getAssignmentQuestionList();
        return Result.success(assignmentQuestionList);
    }
}